import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
	//状态--不能在里边修改
	state: {
		// 声明需要保存的属性
		username: '',
		password: '',
		user_id:'',
		car:[],
		xianshi:false
	},
	//修改状态
	mutations: {
		/* 只能有两个参数 在里边修改state状态*/
		changUser(state, {name, pwd,user_id}) {
			state.username = name;
			state.password = pwd;
			state.user_id = user_id;
		},
		changecar(state,foodscar){
			
			let flag = false;
			
			state.car.some(item =>{
				if(item.name == foodscar.name){
					item.count += foodscar.count
					flag = true;
					return true;
				}
			})
			
			if(!flag){
				state.car.push(foodscar);
			}
			sessionStorage.setItem('car',JSON.stringify(state.car));
		}
	},
	getters:{
		getcount(state){
			let c = 0;
			state.car.forEach(item =>{
				c += item.count;
			})
			return c;
		}
	},
	actions: {},
	modules: {}
});
